﻿#include <stdio.h>
#include <stdlib.h>
#define N 5
struct student
{
	int num;
	char name[10];
	float score[3];
	float aver;
}stu[N];
void save()
{
	FILE *fp;
	int i;
	if ((fp = fopen("stud", "wb")) == NULL)
	{
		printf("cannot open the file stud!\n");
		exit(0);
	}
	for (i = 0; i<N; i++)
	{
		if ((fwrite(&stu[i], sizeof(struct student), 1, fp)) != 1)
			printf("flie write error!\n");
	}
	fclose(fp);
}
int main()
{
	int i;
	printf("请输入学号、姓名、三门课程成绩:\n");
	for (i = 0; i<N; i++)
	{
		scanf("%d %s %f %f %f", &stu[i].num, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);
		stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;
	}
	save();
	FILE *fp;
	if ((fp = fopen("stud", "rb")) == NULL)
	{
		printf("cannot open the file stud!\n");
		exit(0);
	}
	printf("the data:\n");
	printf("num name score1 score2 score3 average\n");
	for (i = 0; i<N; i++)
	{
		fread(&stu[i], sizeof(struct student), 1, fp);
		printf("%-2d%5s%7.1f%7.1f%7.1f%7.1f\n", stu[i].num, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].aver);
	}	
	fclose(fp);
	return 0;
}
